package pl.webapps.alarmme;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class RecordingActivity extends Activity {
    public static final String REC_REPO_NAME_BUNDLE = "rec_repo_name_bundle";
    public static final String REC_CURRENT_NUMBER_BUNDLE = "rec_current_number_bundle";
    private String mRepoName;
    private int mCurrentNumber;
    private TextView mRepoNameLabel;
    private TextView mCurrentNumberLabel;
    private Button mPrevBtn;
    private Button mPreviewBtn;
    private Button mNextBtn;
    private Button mRecBtn;
    private ImageView mExistsImage;
    private TextView mExistsText;
    private boolean mIsRecording;
    private OnClickListener mBtnListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rec_prev_button:
                    decrementCurrentNumber();
                    break;
                case R.id.rec_preview_button:
                    previewCurrent();
                    break;
                case R.id.rec_next_button:
                    incrementCurrentNumber();
                    break;
                case R.id.rec_rec_button:
                    recordCurrent();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recording);
        mIsRecording = false;
        prepareViewItems();
    }

    private void prepareViewItems() {
        mExistsImage = (ImageView) findViewById(R.id.rec_item_exists_image);
        mExistsText = (TextView) findViewById(R.id.rec_item_exists_label);
        mRepoNameLabel = (TextView) findViewById(R.id.rec_repo_text);
        mCurrentNumberLabel = (TextView) findViewById(R.id.rec_current_number_text);
        mPrevBtn = (Button) findViewById(R.id.rec_prev_button);
        mPreviewBtn = (Button) findViewById(R.id.rec_preview_button);
        mNextBtn = (Button) findViewById(R.id.rec_next_button);
        mRecBtn = (Button) findViewById(R.id.rec_rec_button);
        mPrevBtn.setOnClickListener(mBtnListener);
        mPreviewBtn.setOnClickListener(mBtnListener);
        mNextBtn.setOnClickListener(mBtnListener);
        mRecBtn.setOnClickListener(mBtnListener);
        Bundle bun = getIntent().getExtras();
        if (bun != null) {
            mRepoName = bun.getString(REC_REPO_NAME_BUNDLE);
            if (mRepoName == null) {
                mRepoName = "default";
            }
            String num = getIntent().getExtras().getString(REC_CURRENT_NUMBER_BUNDLE);
            if (num == null) {
                mCurrentNumber = 0;
            } else {
                mCurrentNumber = Integer.parseInt(num);
            }
        } else {
            mRepoName = "default";
            mCurrentNumber = 0;
        }
        mRepoNameLabel.setText(mRepoName);
        mCurrentNumberLabel.setText(String.valueOf(mCurrentNumber));
    }

    private void incrementCurrentNumber() {
        if (mCurrentNumber >= 0 && mCurrentNumber <= 19) {
            ++mCurrentNumber;
        } else if (mCurrentNumber == 20 || mCurrentNumber == 30 || mCurrentNumber == 40) {
            mCurrentNumber += 10;
        } else {
            Log.e("RecordingActivity", "can't increment above 50!");
            return;
        }
        mCurrentNumberLabel.setText(String.valueOf(mCurrentNumber));
        validateExistenceOfCurrentNumber();
    }

    private void decrementCurrentNumber() {
        if (mCurrentNumber >= 1 && mCurrentNumber <= 20) {
            --mCurrentNumber;
        } else if (mCurrentNumber == 30 || mCurrentNumber == 40 || mCurrentNumber == 50) {
            mCurrentNumber -= 10;
        } else {
            Log.e("RecordingActivity", "can't decrement below 0!");
            return;
        }
        mCurrentNumberLabel.setText(String.valueOf(mCurrentNumber));
        validateExistenceOfCurrentNumber();
    }

    private void validateExistenceOfCurrentNumber() {
        // TODO check existance
        boolean exists = Math.random() < 0.5 ? true : false;
        if (exists) {
            mExistsText.setText(R.string.rec_item_exists_label_yes);
            mExistsImage.setImageResource(R.drawable.rec_exists_green);
        } else {
            mExistsText.setText(R.string.rec_item_exists_label_no);
            mExistsImage.setImageResource(R.drawable.rec_exists_red);
        }
    }

    private void previewCurrent() {
    }

    private synchronized void recordCurrent() {
        mIsRecording = !mIsRecording;
        if (mIsRecording) {
            mRecBtn.setBackgroundResource(R.drawable.rec_btn_green);
        } else {
            mRecBtn.setBackgroundResource(R.drawable.rec_btn);
        }
    }
}
